package com.starlink.service.impl;

import com.starlink.common.annotation.client.jd.JdOperationRequest;
import com.starlink.common.annotation.client.jd.JdOperationResponse;
import com.starlink.common.enums.JdNodeStateEnum;
import com.starlink.common.enums.LogisticsOrderBusinessEnum;
import com.starlink.common.strategy.JdNotify;
import com.starlink.model.domain.MallLogisticsDO;
import com.starlink.model.domain.MallRefundsDO;
import com.starlink.service.JDTraceCallBackService;
import com.starlink.service.MallLogisticsService;
import com.starlink.service.MallRefundsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.starlink.common.enums.OrderRefundEnum.WAN_CHENG;
import static com.starlink.common.enums.OrderRefundEnum.YUN_SHU;

/**
 * @Auther: zhouyuhang01
 * @Date: 2024/5/21 15:38
 * @Description: JDTraceCallBackServiceImpl
 * @Version 1.0.0
 */
@Slf4j
@Service
public class JDTraceCallBackServiceImpl implements JDTraceCallBackService {
    @Autowired
    private MallLogisticsService logisticsService;
    @Autowired
    private MallRefundsService refundsService;
    @Autowired
    private List<JdNotify> jdNotifies;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JdOperationResponse traceMsgPush(JdOperationRequest req) {
        String state = req.getState();
        String freeOrderId = req.getOrderId();
        if (freeOrderId.contains("SC")){//代表是商城的物流订单号,根据生成的商家物流单号前面两个字母来判断是商城还是书包的物流订单
            //修改物流状态
            logisticsService.lambdaUpdate()
                    .set(MallLogisticsDO::getLogisticsStatus,state)
                    .eq(MallLogisticsDO::getLogisticsNum,freeOrderId)
                    .update();
            //妥投
            if (JdNodeStateEnum.STATE_10034.getCode().equals(state)){
                MallLogisticsDO logisticsDO = logisticsService.lambdaQuery()
                        .eq(MallLogisticsDO::getLogisticsNum, freeOrderId)
                        .one();
                Long scOrderId = logisticsDO.getOrderId();
                Integer orderType = logisticsDO.getOrderType();
                //发货订单修改订单状态为待收货
                for (JdNotify notify : jdNotifies) {
                    if (notify.supports(orderType)){
                        notify.handle(scOrderId);
                    }
                }
            }
        }
        //售后换货订单售后状态改为已完成
        if (freeOrderId.contains("SCSH")){
            MallLogisticsDO logisticsDO = logisticsService.lambdaQuery()
                    .eq(MallLogisticsDO::getLogisticsNum, freeOrderId)
                    .one();
            Long scOrderId = logisticsDO.getOrderId();
            refundsService.lambdaUpdate()
                    .set(MallRefundsDO::getStatus,WAN_CHENG.getType())
                    .eq(MallRefundsDO::getOrderId,scOrderId)
                    .eq(MallRefundsDO::getStatus, YUN_SHU.getType())
                    .update();
        }
        if (freeOrderId.contains(LogisticsOrderBusinessEnum.SMART_WEARABLE.getCode())) {
            // 星健康：智能穿戴
            //修改物流状态
            logisticsService.lambdaUpdate()
                    .set(MallLogisticsDO::getLogisticsStatus,state)
                    .eq(MallLogisticsDO::getLogisticsNum,freeOrderId)
                    .update();
            //妥投
            if (JdNodeStateEnum.STATE_10034.getCode().equals(state)){
                MallLogisticsDO logisticsDO = logisticsService.lambdaQuery()
                        .eq(MallLogisticsDO::getLogisticsNum, freeOrderId)
                        .one();
                Long scOrderId = logisticsDO.getOrderId();
                Integer orderType = logisticsDO.getOrderType();
                //发货订单修改订单状态为待收货
                for (JdNotify notify : jdNotifies) {
                    if (notify.supports(orderType)){
                        notify.handle(scOrderId);
                    }
                }
            }
        }
        if (freeOrderId.contains(LogisticsOrderBusinessEnum.XL.getCode())) {
            // todo 星链书包处理物流回调处理
        }
        return new JdOperationResponse(HttpStatus.OK.value(),"成功");
    }
}
